from machine import Pin
from utime import sleep_ms
import ds1302
from machine import SoftI2C
from ssd1306 import SSD1306_I2C

# OLED显示时间
def oled_show(oled, date_time):
    # 清空显示屏
    oled.fill(0)
    # 显示字符: 字符，坐标，黑白
    week_str = ("Sun", "Mon", "Tues", "Wed", "Thur", "Fri", "Sat")
    oled.text("DS1302 RTC", 22, 5, 1)
    oled.text("%d/%d/%d" % (date_time[0], date_time[1], date_time[2]), 10, 20, 1)
    oled.text("%02d:%02d:%02d %s" %
              (date_time[4], date_time[5], date_time[6], week_str[date_time[3]]), 10, 32, 1)
    # 刷新显示
    oled.show()
    
# DS1302 RTC
def ds1302_time(ds1302, oled):
    while True:
        date_time = ds1302.DateTime()
        oled_show(oled, date_time)
        sleep_ms(1000)


if __name__ == "__main__":
    # 创建OLED对象
    oled = SSD1306_I2C(128, 64, SoftI2C(scl=Pin(18), sda=Pin(23)))
    
    # 创建DS1302对象
    ds1302 = ds1302.DS1302(clk=Pin(14), dio=Pin(27), cs=Pin(26))
    # 初始化时间
    ds1302.DateTime([2024, 6, 28, 5, 15, 13, 35])
    
    ds1302_time(ds1302, oled)
    
    